package servlet;

import Bean.Author;
import Bean.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

@WebServlet(name = "servlet02")
public class servlet02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String area = request.getParameter("area");
        String no = request.getParameter("no");
        Integer number = Integer.parseInt(no);
        JdbcUtil jdbcUtil = new JdbcUtil();
        ArrayList result = new ArrayList();
        //判断有哪种检索依据
        if (name != null){
            try {
                result = jdbcUtil.searchName(name);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else {
            try {
                result = jdbcUtil.searchArea(area);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        ArrayList<String> author = (ArrayList<String>) result.get(0);//[方洁, 龙利方, 方韬, 方涛, 陈方宁, 方东权, 贺德方, 方曙, 方嘉瑶]
        ArrayList<String> address = (ArrayList<String>) result.get(1);//[南京大学信息管理学院, 国家图书馆, 北京师范大学历史学院, 宁夏社会科学院, 南京师范大学图书馆, 华中农业大学图书馆, 中国科学技术信息研究所, 中国科学院成都文献情报中心, 东莞图书馆]
        ArrayList<Integer> pagesum = (ArrayList<Integer>) result.get(2);//[1, 1, 1, 1, 1, 1, 4, 2, 1]
        ArrayList<ArrayList<String>> keywords = (ArrayList<ArrayList<String>>) result.get(3);//[[虚拟团队, 知识共享行为, 知识接受者, 影响因素], [日本国立国会图书馆, 中国学, 博士论文], [裴骃, 史记集解, 左传, 杜预注], [舆情, 引导, Web, 构建], [数字图书馆, 功能转变, 形神分离, 信息技术革命], [电子资源, 模糊决策, 综合评价, 评价体系], [馆藏资源, 语义, 聚合研究, 科技报告, 科技项目管理, 知识管理, 情报, 工程, 科技情报, 研究范式, 科技政策, 制度建设], [个体行为, 科研合作网络, 知识扩散, 模型, 微分动力, 学科研合作网络, 模型影响机制], [4S营销理论, 移动阅读推广, 图书馆]]
        ArrayList<ArrayList<String>> articles = (ArrayList<ArrayList<String>>) result.get(4);//[[基于知识接受者视角的虚拟团队知识共享行为研究], [日本学位论文的中国学研究], [《史记集解》所引《左传》杜预注考], [网络舆情引导及其Web环境的构建], [信息时代下图书馆的数字化和“形神分离”], [基于模糊决策的电子资源定量评价方法探讨——以武汉工程大学图书馆为例], [基于语义的馆藏资源深度聚合研究, 科技报告的内涵、作用与管理机制, 工程化思维下的科技情报研究范式——情报工程学探析, 中国科技报告制度的建设方略], [基于个体行为的科研合作网络知识扩散建模研究, 基于微分动力学的科研合作网络知识扩散模型及影响机制研究], [基于4S营销理论的图书馆移动阅读推广研究——以东莞图书馆为例]]
        ArrayList<ArrayList<String>> collaborators = (ArrayList<ArrayList<String>>) result.get(5);//[[龚立群, 张亚军], [], [], [马晓东], [陈欣慧], [王琼], [曾建勋], [], [], [], [岳增慧, 许海云], [岳增慧, 许海云], []]
        ArrayList time = new ArrayList();
        for (int j = 0; j < author.size() ; j++){
            String singleauthor = author.get(j);
            String singleaddress = address.get(j);
            String singlepagesum = Integer.toString(pagesum.get(j));
            ArrayList<String> singlekeywords = keywords.get(j);
            ArrayList<String> singlearticles = articles.get(j);
            ArrayList<String> singlecoll = collaborators.get(j);
            Author a = new Author(singleauthor,singleaddress,singlepagesum,singlekeywords,singlearticles,singlecoll);
            out.println("姓名：");
            out.println(a.getName());
            out.println("地址：");
            out.println(a.getAddress());
            out.println("论文数：");
            out.println(a.getPages());
            out.println("研究领域：");
            out.println(a.getKeywords());
            out.println("论文：");
            out.println(a.getArticles());
            out.println("合作者：");
            out.println(a.getCollaborator());
            out.println("\n");
            //获取当前时间戳
            Timestamp t = new Timestamp(System.currentTimeMillis());
            time.add(t);
        }

        Cookie cookie01 = new Cookie("areas", URLEncoder.encode(lis01Tostring(keywords.get(number),"&%@"),"utf-8"));
        response.addCookie(cookie01);
        out.println(cookie01.getName()+"--->"+cookie01.getValue());

        Cookie cookie02 = new Cookie("articles",URLEncoder.encode(lis01Tostring(articles.get(number),"&%@"),"utf-8"));
        response.addCookie(cookie02);
        out.println(cookie02.getName()+"--->"+cookie02.getValue());

        Cookie cookie03 = new Cookie("collaborators",URLEncoder.encode(lis01Tostring(collaborators.get(number),"&%@"),"utf-8"));
        response.addCookie(cookie03);
        out.println(cookie03.getName()+"--->"+cookie03.getValue());

//        String c4 = lis02Tostring(keywords);
//        Cookie cookie04 = new Cookie("areas",c4);
//        response.addCookie(cookie04);
//        out.println(cookie04.getName()+"--->"+cookie04.getValue());
//
//        String c5 = lis02Tostring(articles);
//        Cookie cookie05 = new Cookie("papers",c5);
//        response.addCookie(cookie05);
//        out.println(cookie05.getName()+"--->"+cookie05.getValue());
//
//        String c6 = lis02Tostring(collaborators);
//        Cookie cookie06 = new Cookie("collaborators",c6);
//        response.addCookie(cookie06);
//        out.println(cookie06.getName()+"--->"+cookie06.getValue());

//        HttpSession session = request.getSession(true);
//        session.setAttribute("names",lis01Tostring(author,"&%@"));
//        session.setAttribute("address",lis01Tostring(address,"&%@"));
//        session.setAttribute("papersum",lis03Tostring(pagesum,"&%@"));
//        session.setAttribute("area",c4);
//        session.setAttribute("article",c5);
//        session.setAttribute("coll",c6);
//        out.println(session.getAttribute("names"));
//        out.println(session.getAttribute("address"));
//        out.println(session.getAttribute("papersum"));
//        out.println(session.getAttribute("area"));
//        out.println(session.getAttribute("article"));
//        out.println(session.getAttribute("coll"));
    }
    //针对ArrayList<String>的转型
    public String lis01Tostring(ArrayList<String> arrayList,String reg){
        String result = "";
        if (arrayList == null){ return result;}
        if (arrayList.size() == 1){
            result = arrayList.get(0) + reg
            ;
        }else {
            for (int i = 0; i < arrayList.size(); i++){
                result += arrayList.get(i) + reg;
            }
        }
        return result;
    }
    //针对ArrayList<ArrayList<String>>的转型
    public String lis02Tostring(ArrayList<ArrayList<String>> arrayList){
        String result = "";
        ArrayList<String> c = new ArrayList<String>();
        if (arrayList == null){ return result;}
        if (arrayList.size() == 1){
            ArrayList<String> sublis = arrayList.get(0);
            result = lis01Tostring(sublis,";");
        }else {
            for (int i = 0; i < arrayList.size(); i++){
                String subresult = lis01Tostring(arrayList.get(i),";");
                c.add(subresult);
            }
            result = lis01Tostring(c,"&%@");
        }
        return result;
    }
    public String lis03Tostring(ArrayList<Integer> arrayList,String reg){
        String result = "";
        if (arrayList == null){ return result;}
        if (arrayList.size() == 1){
            result = Integer.toString(arrayList.get(0));
        }else {
            for (int i = 0; i < arrayList.size(); i++){
                result += Integer.toString(arrayList.get(i)) + reg;
            }
        }
        return result;
    }
}
